/**
 * @team Dynamic Duo
 * 1. Maayan Keshet
 * 2. Gal Bitensky
 * 
 * dynamic.duo.compilation.team@gmail.com
 */


import java.util.LinkedList;
import java.util.List;

import parser.Parser;
import lex.Lexer;
import lex.Token;


public class Main
{
	public static void main(String[] args)
	{
		try {
			boolean isLibInput = false;

			/*check for legal input and input-type (library or program)
			 *&& is using short-circuit evaluation, hence -L will be checked without segfault
			 */
			if (args.length == 2 && args[1].substring(0,2).equals("-L")) {
				isLibInput = true;
			}
			else if (args.length == 1){
				isLibInput = false;
			}
			else {
				System.out.println("Usage: Main <program-filename> [-L<library-filename>]");
				System.exit(0);
			}

			Lexer progLexer = new Lexer();
			Parser progParser = new Parser(false);
			List<Token> progTokens = new LinkedList<Token>();

			try {
				if (isLibInput){
					Lexer libLexer = new Lexer();
					Parser libParser = new Parser(true);
					List<Token> libTokens = new LinkedList<Token>();

					libLexer.process(args[1].substring(2), libTokens);
					libParser.process(libTokens);
				}
				progLexer.process(args[0], progTokens);
				progParser.process(progTokens);
			} catch (Error e) {
				throw e;
			}	
		}
		catch (Exception e) {
			System.err.println(e);
			e.printStackTrace();
		}	
	}
}